/*
题目：力扣-704-二分查找
连接：https://leetcode.cn/problems/binary-search/description/

*/

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left=0;
        int right = nums.size()-1;
        int mid = (left+right)/2;
        //防止溢出
        //int mid = left+(right-left)/2;
        while(left<=right)
        {
            if(nums[mid]<target)
            {
                left=mid+1;
                
            }
            else if(nums[mid]>target)
            {
                right = mid-1;
            }
            else
            {
                return mid;
            }
            mid = (left+right)/2;
        }
        return -1;
    }
};